 function removeKeyword(obj)
 {
    obj.fadeOut("slow", function () { $(this).remove(); });    
 }
 
 function addMetaField(entrytype)
 {
    selBox = $('#metarows_'+entrytype);
    field = selBox.val();
    selBox.removeOption(field);
    $('#meta_row_'+field+entrytype).fadeIn("slow");
 }

function removeMetaField(entrytype, fieldID, datatype)
{
    selBox = $('#metarows_'+entrytype);
    $('#meta_row_'+fieldID+entrytype).fadeOut("slow",
        function () {    
            switch(datatype)
            {
                case 'st':
                case 'lt':
                case 'il':
				case 'dt':
                    $('#meta_row_'+fieldID+entrytype + ' :input').val('');
                break;
                
                case 'kw':
                    $('#meta_row_'+fieldID+entrytype + ' span.meta_keyword').remove();
                break;
            }
        }
    );
    
    fieldName = $('#meta_name_'+fieldID+entrytype).html();
    selBox.addOption(fieldID, fieldName);
    selBox.sortOptions();
}
 
 $(function () {
 
    $(".meta_field_remove"). click ( function () {
        entrytype = $(this).metadata().type;
        fieldID = $(this).metadata().fieldID;
        datatype = $(this).metadata().datatype;
        removeMetaField(entrytype, fieldID, datatype);
    });
 
    $(".addfield").click( function () {
        entrytype = $(this).metadata().type;
        addMetaField(entrytype);
        return false;
    });   

    $(".meta_keyword_remove").click( function () {
       removeKeyword($(this).parent());
    });

	$.extend(DateInput.DEFAULT_OPTS, {
		stringToDate: function(string) {
			var matches;
			if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
      return new Date(matches[1], matches[2] - 1, matches[3]);
    } else {
      return null;
    };
  },
  dateToString: function(date) {
    var month = (date.getMonth() + 1).toString();
    var dom = date.getDate().toString();
    if (month.length == 1) month = "0" + month;
    if (dom.length == 1) dom = "0" + dom;
    return date.getFullYear() + "-" + month + "-" + dom;
  }
});


	$(".date_input").date_input();

    $(".meta_keyword_add").click( function () {
        fieldID = $(this).metadata().fieldID;
        entrytype = $(this).metadata().type;
        $.prompt('Enter your new keyword below:<br /><input type="text" id="keyword_value" name="value" /><input type="hidden" id="keyword_fieldID" name="fieldID" value="'+fieldID+'" /><input type="hidden" id="keyword_type" name="type" value="'+entrytype+'" />', {
        buttons:{'Add':'add', 'Cancel':'cancel'},
        prefix:'colsJqi',
        show:'slideDown',
        callback: addKeyword
        });   
      }); 
    }
  );  
  
  function addKeyword(v,m) {
    if (v == 'cancel') 
    {
        return false;
    }
    fieldID = m.children("#keyword_fieldID").val();
    keyword = m.children("#keyword_value").val();
    entrytype = m.children("#keyword_type").val();

    $("#meta_kw_"+fieldID+entrytype).createAppend(
        'span', {className: "meta_keyword"}, [
                    'a', {className: "meta_keyword_button meta_keyword_remove"}, "-",
                    'span', {}, "&nbsp;"+keyword.replace(/ /g, '&nbsp;'),
                    'input', {type: "hidden", name: "meta["+fieldID+"][]", value:keyword}
                   ]
    
    );
    // Re-add the click event to the new items
    $(".meta_keyword_remove").click( function () {
       removeKeyword($(this).parent());
    });    
  } 
